package com.xhd.config;

import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Vector;

import com.xhd.action.Action;
import com.xhd.base.log.Log;
import com.xhd.base.log.LogFactory;
import com.xhd.base.scheduler.AsyncProcessMgr;
import com.xhd.base.scheduler.SchedulerProcessThread;
import com.xhd.cache.CacheMgr;
import com.xhd.cache.UseCache;
import com.xhd.server.ServerConfig;
import com.xhd.xml.XMLSerializer;

public class SchedulerConfig implements UseCache{
	  private  HashMap jm150 = null;
	  private HashMap jm151 = null;
	  private String jm152 = null;
	  private boolean jm153 = false;
	  static SchedulerConfig jm16 = null;
	  private static final Log jm9;
	  static Class jm10;

	  static
	  {
	    Class tmp7_4 = jm10;
	    if (tmp7_4 == null)
	    {
	  
	      try
	      {
	        SchedulerConfig.jm10 = Class.forName("com.sinocc.base.config.SchedulerConfig");
	      }
	      catch (ClassNotFoundException localClassNotFoundException)
	      {
	        throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
	      }
	    }
	    jm9 = LogFactory.getLog(tmp7_4);
	  }

	  public static synchronized SchedulerConfig getInstance()
	  {
	    if (jm16 == null)
	      jm16 = new SchedulerConfig();
	    return jm16;
	  }

	  private SchedulerConfig()
	  {
	    jm11();
	    CacheMgr.registerCache(this);
	  }

	  private void jm11()
	  {
	    String str1 = ServerConfig.getConfFilePath("schconf.xml");
	    File localFile = new File(str1);
	    if ((!(localFile.exists())) || (!(localFile.isFile())))
	      return;
	    try
	    {
	      XMLSerializer localXMLSerializer = new XMLSerializer(new FileInputStream(localFile), false);
	      this.jm152 = localXMLSerializer.getData("scheduler-task-class");
	      if ((this.jm152 == null) || (this.jm152.length() < 1))
	        this.jm152 = "com.sinocc.sysmanager.CPCSchTask";
	      else
	        try
	        {
	          Class.forName(this.jm152);
	        }
	        catch (Throwable localThrowable)
	        {
	          jm9.error("schconf.xml param 'scheduler-task-class' error:", localThrowable);
	        }
	      Vector localVector = localXMLSerializer.getCollection("schconf");
	      int i;
	      Object localObject;
	      String str3;
	      String str4;
	      String str5;
	      if ((localVector != null) && (localVector.size() > 0))
	      {
	        i = localVector.size();
	        this.jm150 = new HashMap(i);
	        for (int j = 0; j < i; ++j)
	        {
	          Hashtable localHashtable = (Hashtable)localVector.get(j);
	          localObject = (String)localHashtable.get("class");
	          str3 = (String)localHashtable.get("action");
	          str4 = (String)localHashtable.get("priority");
	          if ((localObject == null) || (((String)localObject).length() < 1) || (str3 == null) || (str3.length() < 1) || (str4 == null))
	            continue;
	          if (str4.length() < 1)
	            continue;
	          str5 = localObject + "." + str3;
	          str5 = str5.toLowerCase();
	          this.jm150.put(str5, str4);
	        }
	        jm9.info("SchConfig count: " + this.jm150.size());
	      }
	      else
	      {
	        this.jm150 = null;
	      }
	      localVector = localXMLSerializer.getCollection("asyncconf");
	      if ((localVector != null) && (localVector.size() > 0))
	      {
	        i = localVector.size();
	        this.jm151 = new HashMap(i);
	        for (int k = 0; k < i; ++k)
	        {
	          localObject = (Hashtable)localVector.get(k);
	          str3 = (String)((Hashtable)localObject).get("class");
	          str4 = (String)((Hashtable)localObject).get("action");
	          if ((str3 == null) || (str3.length() < 1) || (str4 == null))
	            continue;
	          if (str4.length() < 1)
	            continue;
	          str5 = str3 + "." + str4;
	          str5 = str5.toLowerCase();
	          Params localParams = new Params();
	          String   str2 = (String)((Hashtable)localObject).get("max");
	          if ((str2 != null) && (str2.length() > 0))
	            localParams.jm317 = Integer.parseInt(str2);
	          else
	            localParams.jm317 = 1;
	          str2 = (String)((Hashtable)localObject).get("notice");
	          if ((str2 != null) && (str2.length() > 0))
	            localParams.jm318 = Integer.parseInt(str2);
	          else
	            localParams.jm318 = 0;
	          this.jm151.put(str5, localParams);
	        }
	        int k = ServerConfig.ThreadMax / 4;
	        int l = ServerConfig.ThreadMax / 20;
	        String str2 = localXMLSerializer.getData("async-max");
	        if ((str2 != null) && (str2.length() > 0))
	          k = Integer.parseInt(str2);
	        str2 = localXMLSerializer.getData("async-free");
	        if ((str2 != null) && (str2.length() > 0))
	          l = Integer.parseInt(str2);
	        AsyncProcessMgr.getInstance().setMax(k, l);
	        jm9.info("AsyncConfig count: " + this.jm151.size());
	        return;
	      }
	      this.jm151 = null;
	    }
	    catch (Exception localException)
	    {
	      jm9.error("SchedulerConfig.loadConfigFile", localException);
	    }
	  }

	  public boolean isSchTask(Action paramAction)
	  {
	    if (this.jm150 == null)
	      return false;
	    boolean bool = this.jm150.containsKey(paramAction.getFullName());
	    if (bool)
	    {
	      if (!(this.jm153))
	        this.jm153 = SchedulerProcessThread.hasThread();
	      if (!(this.jm153))
	      {
	        jm9.error("No scheduler process thread!");
	        return false;
	      }
	    }
	    return bool;
	  }

	  public boolean isAsync(Action paramAction)
	  {
	    if (this.jm151 == null)
	      return false;
	    return this.jm151.containsKey(paramAction.getFullName());
	  }

	  public int getPriority(Action paramAction)
	  {
	    if (paramAction == null)
	      return 0;
	    if (this.jm150 == null)
	      return 0;
	    String str = (String)this.jm150.get(paramAction.getFullName());
	    if (str != null)
	      return Integer.parseInt(str);
	    return 0;
	  }

	  public int getAsyncMax(Action paramAction)
	  {
	    if (paramAction == null)
	      return 0;
	    if (this.jm151 == null)
	      return 0;
	    Params localParams = (Params)this.jm151.get(paramAction.getFullName());
	    if (localParams != null)
	      return localParams.jm317;
	    return 1;
	  }

	  public int getAsyncNotice(Action paramAction)
	  {
	    if (paramAction == null)
	      return 0;
	    if (this.jm151 == null)
	      return 0;
	    Params localParams = (Params)this.jm151.get(paramAction.getFullName());
	    if (localParams != null)
	      return localParams.jm318;
	    return 0;
	  }

	  public Class getTaskClass()
	  {
	    try
	    {
	      return Class.forName(this.jm152);
	    }
	    catch (Exception localException)
	    {
	    }
	    return null;
	  }

	  public Object cacheData()
	  {
	    return this.jm150;
	  }

	  public int cacheSize()
	  {
	    int i = 0;
	    if (this.jm150 != null)
	      i += this.jm150.size();
	    if (this.jm151 != null)
	      i += this.jm151.size();
	    return i;
	  }

	  public boolean clearCache()
	  {
	    jm11();
	    return true;
	  }

	  class Params
	  {
	    int jm317;
	    int jm318;
	  }
}
